package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.request.ApproveListRequest;
import com.yanchangapp.model.request.RecordListRequest;
import com.yanchangapp.model.response.GoodsApproveResponse;
import com.yanchangapp.model.response.RecordListResponse;
import com.yanchangapp.ui.view.GoodsApproveFgView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-26.
 * description: 采购审批P层
 */
public class GoodsApproveFgPresenter extends BasePresenter<GoodsApproveFgView> {

    public GoodsApproveFgPresenter(BaseActivity context) {
        super(context);
    }

    public void queryApproveList(String status,int curPage,boolean isPulling){
        // 如果是第一页，并且下拉下来的 展示遮盖层
        if (getView() != null && curPage == 1 && !isPulling){
            getView().showCoverDialog();
        }
        ApproveListRequest request = new ApproveListRequest(status,curPage);
        HeadApiRetrofit.getInstance().queryApproveList(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<GoodsApproveResponse>() {
                    @Override
                    public void onNext(GoodsApproveResponse goodsApproveResponse) {
                        if (getView() != null){
                            if (curPage == 1 && !isPulling){
                                getView().hideCoverDialog();
                            }
                            if (AppConstant.REQUEST_SUCCESS == goodsApproveResponse.getCode()){
                                getView().queryApproveListSuccess(goodsApproveResponse.getData().getList());
                            }else {
                                getView().queryApproveListFailure(goodsApproveResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            if (curPage == 1 && !isPulling){
                                getView().hideCoverDialog();
                            }
                            getView().queryApproveListFailure(exception.getErrorInfo());
                        }
                    }
                });

    }
}
